package org.linlinjava.litemall.wx.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCommodity;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.service.LitemallCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品(服务类型)  goods的意思是commodity
 */
@RestController
@RequestMapping("/wx/commodity")
public class WxGoodsController {
    private final Log log= LogFactory.getLog(WxGoodsController.class);
	@Autowired
	private LitemallCommodityService commodityService;

//原来 浩楠的 商品list服务
/*		@PostMapping("list")
	public Object list(@RequestBody String msg) {
		Integer page = JacksonUtil.parseInteger(msg, "page");
		Integer pageSize = JacksonUtil.parseInteger(msg, "pageSize");
		String name = JacksonUtil.parseString(msg, "name");
		String sort = JacksonUtil.parseString(msg, "sort");
		Integer categoryPid = JacksonUtil.parseInteger(msg, "categoryPid");
		List<Integer> categoryId = JacksonUtil.parseIntegerList(msg, "categoryId");
		List<Integer> brandId = JacksonUtil.parseIntegerList(msg, "brandId");
		if (page == null || pageSize == null) {
			return ResponseUtil.badArgument();
		}
		if (page < 0 || pageSize < 0) {
			return ResponseUtil.badArgumentValue();
		}
		PageInfo<LitemallCommodity> pageInfo = commodityService.selectByCondition(page, pageSize, categoryPid, categoryId, brandId, name, sort);
		Map<String, Object> map = new LinkedHashMap<>(2);
		map.put("totalNum", pageInfo.getTotal());
		map.put("list", pageInfo.getList());
		return ResponseUtil.ok(map);
	}*/

	/**
	 * 单一商品(服务)的详情  根据主键生成
	 * @param msg
	 * @return
	 */
	@PostMapping("detail")
	public Object detail(@RequestBody String msg) {
		Integer id = JacksonUtil.parseInteger(msg, "id");
		if (id == null) {
			return ResponseUtil.badArgument();
		}
		if (id < 0) {
			return ResponseUtil.badArgumentValue();
		}
		return ResponseUtil.ok(commodityService.selectById(id));
	}

	/**
	 * 在OrderGoods表中..是1个order对N个goods    生成几行数据..并且返回..但是没有存入数据库
	 * @param msg
	 * @return
	 */
	@PostMapping("buy")
	public Object buy(@RequestBody String msg) {
		List<LitemallOrderGoods> commodityList = JSONObject.parseArray(msg, LitemallOrderGoods.class);
		//生成 商品  在OrderGoods表中..是1个order对N个goods
		List<LitemallOrderGoods> list = commodityService.generateGoods(commodityList);
		return ResponseUtil.ok(list);
	}

	/**魏来与5月 改写 浩楠原来List
	 * 如果前端判断价格排序 发送 "sort":"commodity_price"  倒序 "sort":"commodity_price desc"
	 * 原来msg>>>{"page":1,"pageSize":10,"categoryPid":"","categoryId":[],"brandId":null,"name":"","sort":""}
	 * 现在page, pageSize,brandIdList, categoryIdL1, categoryIdL2, categoryIdL3, name , sort
	 * @param msg
	 * @return
	 */

	private static final int PAGE_OF = 1;
	private static final int PAGEHIDE_OF = 12;
	@PostMapping("list")
	public Object list(@RequestBody String msg) {
        System.out.println("魏来与5月 改写原来List>>>>>>>>>>>>注意:>>>>>>>>>>>>>>>>>>" + msg);
			JSONObject jsonObject = JSONObject.parseObject(msg);
			Integer page = jsonObject.getInteger("page");
		if(null == page || "".equals(page) || page < 0){ page = PAGE_OF;}
		//如果前端不传参数  默认12 条
		Integer pageSize = jsonObject.getInteger("pageSize");
		if(null == pageSize || "".equals(pageSize) || pageSize < 0){ pageSize = PAGEHIDE_OF;}
			Integer categoryIdL1 = jsonObject.getInteger("categoryIdL1");
			Integer categoryIdL2 = jsonObject.getInteger("categoryIdL2");
			Integer categoryIdL3 = jsonObject.getInteger("categoryIdL3");
//			List<Integer> brandIdList = new ArrayList<>();
//			JSONArray brandIds = jsonObject.getJSONArray("brandId");
//			if(null != brandIds){
//				for (Object brandId : brandIds) {
//					brandIdList.add(Integer.parseInt(String.valueOf(brandId)));
//					}}
		//解析不了用
		  List<Integer> brandIdList = JacksonUtil.parseIntegerList(msg, "brandId");
		String name = jsonObject.getString("name");
			String sort = jsonObject.getString("sort");
		PageInfo<LitemallCommodity> pageInfo = commodityService.findByL1L2L3(page, pageSize,brandIdList, categoryIdL1, categoryIdL2, categoryIdL3, name , sort);
		return ResponseUtil.ok(pageInfo);
	}


}